package
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	/**
	 * @author perry
	 */
	public class MicroGZMInput
	{
		public var mouseIsDown:Boolean = false;
		public var mouseClickX:int = 0;
		public var mouseClickY:int = 0;
		public var mouseX:int = 0;
		public var mouseY:int = 0;
		
		public var pressing:Object = { up:0, down:0, left:0, right:0, fire:0 };
		
		public var cameraAngleX:Number = 0;
		public var cameraAngleY:Number = 0;
		public var cameraAngleZ:Number = 0;
		
		public var mouseLookMode:Boolean = true;
		
		public var stage:Stage;
		
		public function MicroGZMInput( theStage:Stage )
		{
			stage = theStage;
			// get keypresses and detect the game losing focus
			stage.addEventListener( KeyboardEvent.KEY_DOWN, keyPressed );
			stage.addEventListener( KeyboardEvent.KEY_UP, keyReleased );
			stage.addEventListener( Event.DEACTIVATE, lostFocus );
			stage.addEventListener( Event.ACTIVATE, gainFocus );
			stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDown );
			stage.addEventListener( MouseEvent.MOUSE_UP, mouseUp );
//			stage.addEventListener( MouseEvent.MOUSE_MOVE, mouseMove );
		}
		
//		private function mouseMove( e:MouseEvent ):void
//		{
//			mouseX = e.stageX;
//			mouseY = e.stageY;
////			if ( mouseIsDown && mouseLookMode )
////			{
////				cameraAngleY = 90 * ( (mouseX - mouseClickX) / stage.stageWidth );
////				cameraAngleX = 90 * ( (mouseY - mouseClickY) / stage.stageHeight );
////			}
//			
//			cameraAngleY = 90 * ( (mouseX - mouseClickX) / stage.stageWidth );
//			cameraAngleX = 90 * ( (mouseY - mouseClickY) / stage.stageHeight );
//		}
		
		private function mouseDown( e:MouseEvent ):void
		{
			mouseClickX = e.stageX;
			mouseClickY = e.stageY;
			mouseIsDown = true;
		}
		
		private function mouseUp( e:MouseEvent ):void
		{
			mouseIsDown = false;
			
//			if ( mouseLookMode )
//			{
//				cameraAngleX = cameraAngleY = cameraAngleZ = 0;
//			}
		}
		
		private function keyPressed( e:KeyboardEvent ):void
		{
			// qwer 81 87 69 82
			// asdf 65 83 68 70
			// left right 37 39
			// up down 38 40
			//trace("keyPressed " + event.keyCode);
			switch( e.keyCode )
			{
				case Keyboard.UP:
				case 87: // the W key
					pressing.up = true;
				break;
					case Keyboard.DOWN:
				case 83: // the S key
					pressing.down = true;
				break;
				case Keyboard.LEFT:
				case 65: // the A key
					pressing.left = true;
				break;
				case Keyboard.RIGHT:
				case 68: // the D key
					pressing.right = true;
				break;
				case Keyboard.SPACE:
					pressing.fire = true;
				break;
			}
		}
		
		private function keyReleased( e:KeyboardEvent ):void
		{
			switch( e.keyCode )
			{
				case Keyboard.UP:
				case 87:
					pressing.up = false;
				break;
				case Keyboard.DOWN:
				case 83:
					pressing.down = false;
				break;
				case Keyboard.LEFT:
				case 65:
					pressing.left = false;
				break;
				case Keyboard.RIGHT:
				case 68:
					pressing.right = false;
				break;
				case Keyboard.SPACE:
					pressing.fire = false;
				break;
			}
		}
		
		private function gainFocus(event:Event):void
		{
			trace("Game received keyboard focus.");
		}
		// if the game loses focus, don't keep keys held down
		private function lostFocus(event:Event):void
		{
			trace("Game lost keyboard focus.");
			pressing.up = false;
			pressing.down = false;
			pressing.left = false;
			pressing.right = false;
			pressing.fire = false;
		}
	}
}
